package day05

import "sort"

// day05
//
// 455. 分发饼干
//
// https://leetcode.cn/problems/assign-cookies/
//
// ```go
func findContentChildren(g []int, s []int) int {
	sort.Ints(g)
	sort.Ints(s)
	ans := 0
	for i, j := 0, 0; i < len(s) && j < len(g); i++ {
		if s[i] >= g[j] {
			ans++
			j++
		}
	}
	return ans
}

//```

// 376. 摆动序列
//
// https://leetcode.cn/problems/wiggle-subsequence/
//
// ```go
func wiggleMaxLength(nums []int) int {
	down, up := 1, 1
	for i := 1; i < len(nums); i++ {
		if nums[i] > nums[i-1] {
			down = up + 1
		} else if nums[i] < nums[i-1] {
			up = down + 1
		}
	}
	return max(down, up)
}
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

// ```
//
// 53. 最大子数组和
//
// https://leetcode.cn/problems/maximum-subarray/
//
// ```go
func maxSubArray(nums []int) int {
	sum, maxn := 0, -0x3f3f3f3f
	for i := 0; i < len(nums); i++ {
		sum += nums[i]
		if sum > maxn {
			maxn = sum
		}
		if sum < 0 {
			sum = 0
		}
	}
	return maxn
}

//```
